/*
    The I Ching (the Book of Changes) plasmoid.
    Copyright (C) 2002-2009 Jean Pierre Charalambos.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "iching.h"
#include <iostream>

// Qt
#include <QGraphicsLinearLayout>
#include <QToolButton>

// kde
//#include <KIcon>
//#include <KIconLoader>
//#include <KToolInvocation>
#include <KLocale>
//#include <KLineEdit>
#include <KConfigDialog>

// Plasma
#include <Plasma/TabBar>
#include <Plasma/WebView>
#include <Plasma/SpinBox>
//test:
#include <Plasma/ToolButton>
//#include <Plasma/ScrollWidget>
#include <Plasma/Label>
#include <Plasma/ComboBox>
#include <Plasma/Frame>
#include <Plasma/LineEdit>
// own
#include "hexagram.h"
#include "relationship.h"
#include "linebuttongroupgenerator.h"
#include "trigramselectorgenerator.h"

iching::iching ( QObject *parent, const QVariantList &args )
        : Plasma::PopupApplet ( parent, args ),
        m_graphicsWidget ( 0 ),
        tabs ( 0 ) {
    // this will get us the standard applet background, for free!
    setHasConfigurationInterface ( true );
    setPopupIcon ( "help-contents" );//absolutely necessary!!!
    setAspectRatioMode ( Plasma::IgnoreAspectRatio );
}

iching::~iching() {
    if ( hasFailedToLaunch() ) {
        // Do some cleanup here
    } else {
        //writeConfig();//crash?
        delete hexagram;
        delete relation;
        delete lineGenerator;
        //delete hexWebTab;
        //delete hexGenTab;
        delete tabs;
    }
}

QGraphicsWidget *iching::graphicsWidget() {
    if ( !m_graphicsWidget ) {
        m_graphicsWidget = new QGraphicsWidget ( this );
        m_graphicsWidget->setPreferredSize ( 400,300 );
        QGraphicsLinearLayout *layout = new QGraphicsLinearLayout ( Qt::Horizontal, m_graphicsWidget );

        //creates generator tab widget
        lineGenerator = new TrigramSelectorGenerator ( hexagram, this );

        tabs = new Plasma::TabBar ( this );

        hexWebTab = new Plasma::WebView ( tabs );
        hexWebTab->setSizePolicy ( QSizePolicy::Expanding, QSizePolicy::Expanding );
        KUrl url ( "http://iching.sf.net/Documentation/design/design-one/index.html" );
        hexWebTab->setUrl ( url );

        //adds the tabs
        tabs->addTab ( i18n ( "Generate" ), lineGenerator );
        tabs->addTab ( i18n ( "Text" ), hexWebTab );

        layout->addItem ( tabs );
    }

    return m_graphicsWidget;
}

void iching::init() {
    srand ( time ( NULL ) );
    hexagram = new Hexagram ( );
    relation = new Relationship ( hexagram );
    test();
    //readConfig();
}

/**
void iching::readConfig() {
    KConfigGroup cg = config();
    QString a = cg.readEntry ( "defaulttranslator" );
}

void iching::writeConfig() {
    KConfigGroup cg = config();
    if ( ts_ui.translatorbuttongroup->selected() == 0 )
        translator = "Wilhelm";
    else
        translator = "Legge";
    cg.writeEntry( "defaulttranslator", translator );
    emit configNeedsSaving();
}

void iching::configAccepted() {
    std::cout<<"selected id button ID: "<< ts_ui.translatorbuttongroup->selected() << std::endl;
    writeConfig();
}
// */

void iching::createConfigurationInterface ( KConfigDialog *parent ) {
    QWidget *translatorSelectorForm = new QWidget;
    ts_ui.setupUi ( translatorSelectorForm );
    parent->addPage ( translatorSelectorForm, i18n ( "TranslatorSelector" ) );
    //connect ( parent, SIGNAL ( applyClicked() ), this, SLOT ( configAccepted() ) );
    //connect ( parent, SIGNAL ( okClicked() ), this, SLOT ( configAccepted() ) );
}

void iching::test() {
    //test
    Hexagram * hex1 = new Hexagram ( 64 );
    Hexagram * hex2 = new Hexagram ( 24 );
    hex1->setId ( 64 );
    Relationship * rel = new Relationship ( hex1 );
    hex1->setChangingLine ( 1 );
    hex1->setChangingLine ( 4 );
    hex2->setChangingLine ( 0 );
    hex2->setChangingLine ( 4 );
    hex2->setChangingLine ( 5 );
    std::cout<<"Hex ID: "<< hex1->getId()
             <<"; 2nd. Hex: "<< rel->getSecondHexagramID()
             <<"; Seq. Hex: "<< rel->getSequenceHexagramID()
             <<"; Constrast Hex: "<< rel->getConstrastHexagramID()
             <<"; Shadow Hex: "<< rel->getShadowHexagramID()
             <<"; Nuclear Hex: "<< rel->getNuclearHexagramID()
             << std::endl;
    int size = rel->getStepOfChange().size();
    for ( int i=0; i<size ;i++ )
        std::cout<<"Id: "<< i << ": "<< rel->getStepOfChange().at ( i ) <<" ";
    std::cout<<std::endl;
    rel->setRefHexagram ( hex2 );
    std::cout<<"Hex ID: "<< hex2->getId()
             <<"; 2nd. Hex: "<< rel->getSecondHexagramID()
             <<"; Seq. Hex: "<< rel->getSequenceHexagramID()
             <<"; Constrast Hex: "<< rel->getConstrastHexagramID()
             <<"; Shadow Hex: "<< rel->getShadowHexagramID()
             <<"; Nuclear Hex: "<< rel->getNuclearHexagramID()
             << std::endl;
    size = rel->getStepOfChange().size();
    for ( int i=0; i<size ;i++ )
        std::cout<<"Id: "<< i << ": "<< rel->getStepOfChange().at ( i ) <<" ";
    std::cout<<std::endl;
}

#include "iching.moc"
